import Vue from 'vue'
import App from './App'
import router from './router'
// import mui from './lib/js/mui.min.js'
import axios from 'axios'
import 'babel-polyfill'
import Vuex from 'vuex'

import { Tabbar, TabItem, Navbar, TabContainer, TabContainerItem, Field, MessageBox, InfiniteScroll, Loadmore, DatetimePicker, Swipe, SwipeItem } from 'mint-ui'
import 'mint-ui/lib/style.css'
import 'lib-flexible/flexible'
import 'font-awesome/css/font-awesome.min.css'
import './lib/css/mint.scss'

Vue.use(Vuex)
Vue.prototype.$axios = axios
// Vue.prototype.mui = mui
Vue.config.productionTip = false
// import Mint from 'mint-ui'
// Vue.use(Mint)
Vue.component(Tabbar.name, Tabbar)
Vue.component(TabItem.name, TabItem)
Vue.component(Navbar.name, Navbar)
Vue.component(TabContainer.name, TabContainer)
Vue.component(TabContainerItem.name, TabContainerItem)
Vue.component(Field.name, Field)
Vue.component(Loadmore.name, Loadmore)
Vue.component(DatetimePicker.name, DatetimePicker)
Vue.component(Swipe.name, Swipe)
Vue.component(SwipeItem.name, SwipeItem)
Vue.use(InfiniteScroll)
// axios.defaults.baseURL = '/api'
// axios.defaults.baseURL = 'http://xxn.chang0bj.com'
axios.defaults.baseURL = ''
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// axios.post("api/queryUserSession").then(response=>{
//   if(response.data.code==200){
//       localStorage.setItem("userInfo",JSON.stringify(response.data.object.userInfo))
//    }
// })
axios.interceptors.request.use(config => {
  if (config.method == 'post' &&
    config.headers.post['Content-Type'] == 'application/json;charset=UTF-8' &&
    config.data != undefined &&
    config.data != null &&
    typeof config.data !== 'string') {
    let formData = new FormData()
    let datas = Object.entries(config.data)
    for (let index = 0; index < datas.length; index++) {
      formData.append(datas[index][0], datas[index][1])
    }
    config.data = formData
  }
  return config
})

axios.interceptors.response.use(response => {
  switch (response.status) {
    case 200:
      if (response.data != null) {
        // console.log(response.data.code)
        if (response.data.code != undefined) {
          if (response.data.code != 200 && response.data.code != 505 && response.data.code != 556 && response.data.code != 502 && response.data.code != 516 && response.data.code != 0 && response.data.code != 300) {
            if (response.data.code == 401) {
              MessageBox.alert(`尊敬的用户${response.data.msg}`).then(action => {
                localStorage.clear()
                window.location.href = '/LoginIndex'
              })
            } else {
              MessageBox.alert(`尊敬的用户${response.data.msg}`)
            }
          }
        }
      }
      break
    default:

      // Vue.prototype.$message({ message: "[" + response.status + "]" + response.statusText, type: "warning" });
      MessageBox.alert(`尊敬的用户${response.data.msg}`).then(action => {
        localStorage.clear()
        window.location.href = '/loginIndex'
      })
  }
  return response
}, error => {
  // if (!error.response) {
  //   // Vue.prototype.$message({ message: error.message, type: "error" });
  //   MessageBox('提示', `系统升级维护中,请5分钟后重试${error.message}`);
  //   return;
  // }
  switch (error.response.status) {
    case 504:
      // Vue.prototype.$message({ message: "[" + error.response.status + "]系统繁忙未响应，请重试", type: "warning" });
      MessageBox('提示', `尊敬的用户系统错误${error.response.status}系统繁忙未响应，请重试`)
      break
    default:
      // Vue.prototype.$message({ message: "[" + error.response.status + "]" + error.response.statusText, type: "error" });
      MessageBox('提示', `尊敬的用户系统错误${error.response.status}系统繁忙未响应，请重试`)
  }
})

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
